package com.zhongwang.mms.module.buy.listner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.fastjson.JSON;
import com.zhongwang.mms.common.ImportCallBack;
import com.zhongwang.mms.gen.entity.BReportPlan;
import com.zhongwang.mms.gen.mapper.BReportPlanChangeMapper;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.ExportAssignModel;
import com.zhongwang.mms.module.buy.model.ExportDeferralErrorLogModel;
import com.zhongwang.mms.module.buy.model.ExportDeferralModel;
import com.zhongwang.mms.util.BigDecimalUtils;
import com.zhongwang.mms.util.ShiroUtils;
import com.zhongwang.mms.util.StringUtils;

import java.util.*;

/**
 * @Description: 提报计划导入并修改 listener
 * @Author: China.wsg
 * @Date: 2020/01/09 16:38
 */
public class EditAssignReportPlanListener extends AnalysisEventListener<ExportAssignModel> {

    private ReportPlanDao reportPlanDao;
    private ImportCallBack callBack;
    private List<ExportAssignModel> allDataList = new ArrayList<>();
    private List<ExportAssignModel> correctDataList = new ArrayList<>();
    private List<ExportAssignModel> errorDataList = new ArrayList<>();
    private BReportPlanChangeMapper bReportPlanChangeMapper;
    private List<Integer> exceptionIndexList = new ArrayList<>();
    private int dataIndex = 0;
    /**
     * @name: operationTime
     * @description: 操作时间
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 13:26
     * @version: v1.0
     **/
    private Date operationTime;
    /**
     * @name: operatorId
     * @description: 操作用户主键
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 13:26
     * @version: v1.0
     **/
    private Integer operatorId;

    public EditAssignReportPlanListener(
            ReportPlanDao reportPlanDao,
            ImportCallBack callBack,
            BReportPlanChangeMapper bReportPlanChangeMapper,
            Integer operatorId) {
        this.reportPlanDao = reportPlanDao;
        this.callBack = callBack;
        this.bReportPlanChangeMapper = bReportPlanChangeMapper;
        this.operationTime = new Date();
        this.operatorId = operatorId;
    }

    /**
     * @name: invoke
     * @description: 第一次过滤验证和获取第二次过滤验证需要的数据
     * @param: [data, context]
     * @returnType: void
     * @author: 徐一贺
     * @coddingDate: 2020/8/20 15:44
     * @version: v1.0
     **/
    @Override
    public void invoke(ExportAssignModel data, AnalysisContext context) {
        data.setErrorInfo("");
        StringBuffer errorMessage = new StringBuffer();
        if (data.getRppAssignDesc() == null || StringUtils.replaceBlank(data.getRppAssignDesc()).equals("")) {
            errorMessage.append("指定物料不可为空！");
        }
        if (data.getRppAssignUseStockPrice() == null) {
            errorMessage.append("单价不可为空！\r\n");
        }
        if (data.getRppDiyAssignUseStockNum() == null) {
            errorMessage.append("数量不可为空！\r\n");
        }
        if (data.getRppDiyAssignUseStockUnit() == null || StringUtils.replaceBlank(data.getRppDiyAssignUseStockUnit()).equals("")) {
            errorMessage.append("单位不可为空！\r\n");
        }
        if (errorMessage.toString().length() > 0) {
            data.setErrorInfo(errorMessage.toString());
            errorDataList.add(data);
        } else {
            correctDataList.add(data);
        }
        allDataList.add(data);
        dataIndex++;
    }

    /**
     * @name: doAfterAllAnalysed
     * @description: 开始执行二次过滤验证和操作
     * @param: [context]
     * @returnType: void
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 11:08
     * @version: v1.0
     **/
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        for (int i = 0; i < correctDataList.size(); i++) {
            BReportPlan reportPlan = reportPlanDao.getById(correctDataList.get(i).getRppId());
            BReportPlan entity = new BReportPlan();
            entity.setRppId(correctDataList.get(i).getRppId());
            entity.setRppAssignDesc(correctDataList.get(i).getRppAssignDesc());
            entity.setRppAssignUseStockPrice(correctDataList.get(i).getRppAssignUseStockPrice());
            entity.setRppDiyAssignUseStockNum(correctDataList.get(i).getRppDiyAssignUseStockNum());
            entity.setRppDiyAssignUseStockUnit(correctDataList.get(i).getRppDiyAssignUseStockUnit());
            if (entity.getRppDiyAssignUseStockNum().doubleValue() != reportPlan.getRppNum().doubleValue() ||
                    !entity.getRppDiyAssignUseStockUnit().equals(reportPlan.getRppUnit())) {
                entity.setRppAssignUseStockPrice(null);
            }
            entity.setRppAssignDate(operationTime);
            entity.setRppIsFlag(1);
            reportPlanDao.updateBReportPlanByPrimaryKeySelective(entity);
        }
        //如果有错误数据则记录到错误日志表
        if (errorDataList.size() > 0) {
            callBack.onWarning(errorDataList);
        } else {
            callBack.onSuccess();
        }
    }

    /**
     * @name: onException
     * @description: 异常信息处理
     * @param: [exception 异常, context 内容]
     * @returnType: void
     * @author: 徐一贺
     * @coddingDate: 2020/8/21 11:13
     * @version: v1.0
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exception.printStackTrace();
        exceptionIndexList.add(dataIndex);
        dataIndex++;
//        Map<Integer, Object> map = (LinkedHashMap<Integer, Object>) context.readRowHolder().getCurrentRowAnalysisResult();
//        ExportAssignModel exportAssignModel = JSON.parseObject(JSON.toJSONString(map), ExportAssignModel.class);
//        ExcelDataConvertException edce = (ExcelDataConvertException) exception;
//        switch (edce.getColumnIndex()) {
//            //单价
//            case 24:
//                exportAssignModel.setErrorInfo(exportAssignModel.getErrorInfo()+"单价格式不正确，应为数字。");
//                break;
//            //数量
//            case 25:
//                exportAssignModel.setErrorInfo(exportAssignModel.getErrorInfo()+"数量格式不正确，应为数字。");
//                break;
//        }
//        errorDataList.add(exportAssignModel);
    }
}